package com.lushuan.ch11_springboot_test.demo.threadpool;


public class VolatileDemo {

//    private static int counter = 0;
    private volatile  static int counter = 0;

    public static void main(String[] args) {

        Thread t1= new Thread(()->{
            int localCounter = counter;
            while (counter < 10) {
                if (localCounter != counter) {
                    System.out.println("Thread 1 we see the counter value has changed ");
                    localCounter = counter;
                }
            }
        });

        Thread t2= new Thread(()->{
            int localCounter = counter;
            while (localCounter < 10) {
                localCounter++;

                System.out.println("Thread 2 we see the counter value has changed "+ localCounter);
                 counter = localCounter;

                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t1.start();
        t2.start();




    }



}
